﻿package com.pandemicode.framework.controls {
	import com.pandemicode.framework.interfaces.IDisposeable;
	import com.pandemicode.framework.utils.Disposer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextFieldAutoSize;
	
	/**
	* @author Michael Lawrence
	* @version 1.0.0
	*/
	public class TraceWindow extends Sprite implements IDisposeable {
		private var _output:DefaultTextField;
		private var _background:Shape;
		private var _width:Number;
		private var _height:Number;
		private var _backgroundColor:uint;
		private var _backgroundBorderColor:uint;
		private var _backgroundBorderWidth:Number;
		private var _padding:Number;
		private var _ellipseSize:Number;
		
		/**
		 * The background color of the window.
		 */
		public function set backgroundColor(value:uint):void {
			this._backgroundColor = value;
			this._draw();
		}
		
		/**
		 * The border color of the background.
		 */
		public function set backgroundBorderColor(value:uint):void {
			this._backgroundBorderColor = value;
			this._draw();
		}
		
		/**
		 * The thickness of the background's border.
		 */
		public function set backgroundBorderWidth(value:Number):void {
			this._backgroundBorderWidth = value;
			this._draw();
		}
		
		/**
		 * The padding from the text and the background.
		 */
		public function set padding(value:Number):void {
			this._padding = value;
			this._draw();
		}
		
		/**
		 * The size of the rounded corners of the background.
		 */
		public function set ellipseSize(value:Number):void {
			this._ellipseSize = value;
			this._draw();
		}
		
		/**
		 * The width of the window.
		 */
		override public function get width():Number { return this._width; }
		override public function set width(value:Number):void {
			this._width = value;
			this._draw();
		}
		
		/**
		 * The height of the window.
		 */
		override public function get height():Number { return this._height; }
		override public function set height(value:Number):void {
			this._height = value;
			this._draw();
		}
		
		/**
		 * A window for tracing output.
		 * @param	fontSize				The size of the text.
		 * @param	fontColor				The color of the text.
		 * @param	font					The font for the text.
		 * @param	embedFonts				Whether to embed fonts.
		 * @param	width					The width of the window.
		 * @param	height					The height of the window.
		 * @param	backgroundColor			The background color of the window.
		 * @param	backgroundBorderColor	The border color of the background.
		 * @param	backgroundBorderWidth	The thickness of the background's border.
		 * @param	padding					The padding from the text and the background.
		 * @param	ellipseSize				The size of the rounded corners of the background.
		 */
		public function TraceWindow(fontSize:Number = 10, fontColor:uint = 0xff000000, font:String = "Arial", embedFonts:Boolean = false, width:Number = 100, height:Number = 100, backgroundColor:uint = 0xffcccccc, backgroundBorderColor:uint = 0xff333333, backgroundBorderWidth:Number = 3, padding:Number = 5, ellipseSize:Number = 20) {
			this._background = new Shape();
			this._output = new DefaultTextField(fontSize, fontColor, font, embedFonts, true, TextFieldAutoSize.NONE);
			
			this._width = width;
			this._height = height;
			this._backgroundColor = backgroundColor;
			this._backgroundBorderColor = backgroundBorderColor;
			this._backgroundBorderWidth = backgroundBorderWidth;
			this._padding = padding;
			this._ellipseSize = ellipseSize;
			
			this.addChild(this._background);
			this.addChild(this._output);
			
			this._draw();
		}
		
		/**
		 * Resizes and positions the elements within.
		 */
		private function _draw():void {
			this._background.graphics.clear();
			this._background.graphics.beginFill(this._backgroundColor);
			this._background.graphics.lineStyle(this._backgroundBorderWidth, this._backgroundBorderColor);
			this._background.graphics.drawRoundRect(0, 0, this.width, this.height, this._ellipseSize, this._ellipseSize);
			this._background.graphics.endFill();
			
			this._output.width = this.width - (this._padding * 2);
			this._output.height = this.height - (this._padding * 2);
			this._output.x =
			this._output.y = this._padding;
		}
		
		/**
		 * Disposes of the object.
		 */
		public function dispose():void {
			Disposer.dispose(this._output, this._background);
			this._output = null;
			this._background = null;
		}
		
		/**
		 * Outputs the supplied parameters.
		 * @param	...params	The parameters to write.
		 */
		public function write(...params):void {
			var scrollOK:Boolean = (this._output.scrollV == this._output.maxScrollV);
			
			for (var i:int = 0; i < params.length; i++)
				this._output.text += params[i] + "\n";
				
			if(scrollOK)
				this._output.scrollV = this._output.maxScrollV;
		}
		
		/**
		 * Clears the output.
		 */
		public function clear():void {
			this._output.text = "";
		}
	}
}
